package com.lhkj.ct.admin.modules.scheduling.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.common.enums.DiagItemType;
import com.lhkj.ct.meta.modules.gauge.model.entity.TblDiagItem;
import com.lhkj.ct.meta.modules.gauge.service.DiagItemService;
import com.lhkj.ct.meta.modules.scheduling.common.enums.AppointStatus;
import com.lhkj.ct.meta.modules.scheduling.model.dto.ScheduleSearch;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheAppoint;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheduling;
import com.lhkj.ct.meta.modules.scheduling.model.vo.SchedulingDateVo;
import com.lhkj.ct.meta.modules.scheduling.model.vo.SchedulingDoctorVo;
import com.lhkj.ct.meta.modules.scheduling.service.ScheAppointService;
import com.lhkj.ct.meta.modules.scheduling.service.SchedulingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 预约记录表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-10-29
 */
@RestController
@RequestMapping("api/plat/scheduling/appoint")
@Api(tags = {"4.2预约管理"})
@Validated
public class ScheAppointController {

    @Resource
    private ScheAppointService appointService;

    @Resource
    private SchedulingService schedulingService;

    @Resource
    private DiagItemService diagItemService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "分页查询")
    @GetMapping("list")
    public ActionResult<IPage<TblScheAppoint>> list(TblScheAppoint appoint, PageQuery pageQuery) {
        return ActionResult.success(appointService.selectAppointPage(appoint, pageQuery));
    }

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "用户预约记录-分页", notes = "patId:档案id必传,")
    @GetMapping("histories")
    public ActionResult<IPage<TblScheAppoint>> selectHistoriesByUser(@Validated(ValGroups.User.class) TblScheAppoint appoint, PageQuery pageQuery) {
        return ActionResult.success(appointService.selectAppointPage(appoint, pageQuery));
    }

    @GetMapping(value = "options")
    @ApiOperation(value = "项目下拉选项")
    @ApiOperationSupport(order = 1)
    public ActionResult<List<Option<String>>> options(TblDiagItem entity) {
        entity.setItemType(DiagItemType.TREAT);
        return ActionResult.success(diagItemService.listOptions(entity));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班日期列表")
    @GetMapping("scheDates")
    public ActionResult<List<SchedulingDateVo>> selectScheDates(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheDates(search));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班医生列表")
    @GetMapping("scheDocs")
    public ActionResult<List<SchedulingDoctorVo>> selectScheDoctor(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheDoctor(search));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "预约-排班时间列表")
    @GetMapping("scheTimes")
    public ActionResult<List<TblScheduling>> selectScheTimes(ScheduleSearch search) {
        return ActionResult.success(schedulingService.selectScheTimes(search));
    }

    @PostMapping("insert")
    @ApiOperation(value = "新增预约")
    public ActionResult<Void> appoint(@Validated(ValGroups.Insert.class) @RequestBody TblScheAppoint entity) {
        entity.setStatus(AppointStatus.UNSIGNED);
        appointService.insert(entity);
        return ActionResult.success("预约成功");
    }

    @PutMapping("sign")
    @ApiOperation(value = "签到")
    public ActionResult<Void> sign(@NotEmpty(message = "请选择预约记录") @RequestBody String[] ids) {
        int count = appointService.updateState(AppointStatus.SIGNED,ids);
        return ActionResult.success("签到成功，共签到【" + count + "】条预约记录");
    }

    @PutMapping("done")
    @ApiOperation(value = "治疗(完成)")
    public ActionResult<Void> done(@NotEmpty(message = "请选择预约记录") @RequestBody String[] ids) {
        int count = appointService.updateState(AppointStatus.DONE,ids);
        return ActionResult.success("操作成功，共完成【" + count + "】条预约记录");
    }

    @PutMapping("cancel")
    @ApiOperation(value = "取消")
    public ActionResult<Void> cancel(@NotEmpty(message = "请选择预约记录") @RequestBody String[] ids) {
        int count = appointService.updateState(AppointStatus.CANCEL,ids);
        return ActionResult.success("操作成功，共取消【" + count + "】条预约记录");
    }

}
